import { createRouter, createWebHistory } from 'vue-router'
import TodoList from '@/components/todos/TodoList.vue'
import GoodsShow2 from '@/components/GoodsShow2.vue'
import GoodsShow from '@/components/GoodsShow.vue'
import students from '@/components/Student.vue'
import weather from '@/components/WeatherApp.vue'
import Login from '@/components/axios/Login.vue'
import Friend from '@/components/friends/Friend.vue'
import About from '@/components/nestedrouter/About.vue'
import NewGoods from '@/components/nestedrouter/NewGoods.vue'
import HotGoods from '@/components/nestedrouter/HotGoods.vue'
import NestRoute from '@/components/nestedrouter/NestRoute.vue'
import Composed from '@/components/Composed.vue'
import Cout from '@/components/Cout.vue'
import Shopcart from '@/components/shopcart/Shopcart.vue'
import List from '@/components/shopcart/List.vue'
import Cart from '@/components/shopcart/Cart.vue'
import Student from '@/components/students/StudentList.vue'
//创建路由器
const router = createRouter({
  //history模式，webhash,webhistory
  history: createWebHistory(),
  //路由表
  routes: [
    {
      //根路径
      path: '/',
      // component:TodoList,
      redirect: '/login',
    },
    {
      path: '/sc',
      component: Shopcart,
      children: [
        {
          path: '/sc/list',
          name: 'list',
          meta: {
            title: '商品列表',
          },
          component: List,
        },
        {
          path: '/sc/cart',
          name: 'cart',
          meta: {
            title: '购物车',
          },
          component: Cart,
        },
      ],
    },
    {
      path: '/c',
      component: Composed,
    },
    {
      path: '/student',
      component: Student,
    },
    {
      path: '/cout',
      component: Cout,
    },
    {
      path: '/login',
      component: Login,
      name: 'login',
    },
    {
      path: '/goodsshow',
      component: GoodsShow,
    },
    {
      path: '/goodsshow2',
      component: GoodsShow2,
    },
    {
      path: '/todo',
      component: TodoList,
      name: 'todo',
      meta: {
        title: '待办事项',
        requiredAuth: true,
      },
    },
    {
      path: '/friend',
      component: Friend,
    },
    {
      //动态路由，在路由的路径上添加动态参数的路由是动态路由
      path: '/weather/:cityName',
      component: weather,
      name: 'weather',
      meta: {
        title: '天气信息',
        requiredAuth: true,
      },
    },
    {
      path: '/student',
      component: students,
    },
    {
      path: '/about',
      component: About,
    },
    {
      path: '/new',
      component: NewGoods,
    },
    {
      path: '/hot',
      component: HotGoods,
    },
    {
      path: '/nest',
      component: NestRoute,
      name: 'nest',
      meta: {
        title: '嵌套路由',
        requiredAuth: true,
      },
      children: [
        {
          path: 'about',
          name: 'about',
          meta: {
            title: '关于我们',
          },
          component: About,
        },
        {
          path: 'new',
          name: 'new',
          meta: {
            title: '最新商品',
          },
          component: NewGoods,
        },
        {
          path: 'hot',
          name: 'hot',
          meta: {
            title: '热门商品',
          },
          component: HotGoods,
        },
      ],
    },
  ],
})

router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title
  }
  //1.判断to是否需要授权，如果需要
  //2.检查用户是否登录？
  //3.已经登录，放行
  //4.没有登录，跳转到login
  //5.不需要授权，直接放行,to.matched.meta.requiredAuth
  //java web如何判断用户已经登录？ session
  //前端如何判断？                localstorage, localsession
  if (to.matched.some((item) => item.meta.requiredAuth == true)) {
    //需要认证
    const token = localStorage.getItem('token')
    if (token) {
      //用户已经登录
      next()
    } else {
      //没有登录
      next({
        name: 'login',
        query: {
          path: to.fullPath,
        },
      })
    }
  } else {
    //不需要认证
    next()
  }
})
//输出路由器
export default router
